/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdRegistrarFuncion.java
 *
 * Created on 13-ago-2010, 12:15:15
 */

package GestionRoles;


import administracion.BeanBinding.TblBeanBindingFunciones;
import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Listas;
import administracion.helper.MiTablePrint;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.Utiles_Tabla;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;import seguridad.administracion.ejb30.TO.SistemaTO;
;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.FuncionRolTO;

/**
 *
 * @author Diana
 */
public class jdAdministrarFuncion extends javax.swing.JDialog {

    jdAdministrarRoles padre;
     List listasistema = null;
     List<FuncionRolTO> funciones;
     List<FuncionRolTO>funcionestodas;
     private JTableBinding jTableBinding;

    public jdAdministrarFuncion (java.awt.Frame parent, boolean modal,jdAdministrarRoles padre ) {
        super(parent, modal);
        this.padre=padre;
        initComponents();
        try {
            funcionestodas = GestionSeguridadDelegate.getInstance().getListaFunciones();
        } catch (Exception ex) {
            Logger.getLogger(jdAdministrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
        }
        llenarComboSistema();

    }
    public List<FuncionRolTO> getFuncion_sistema(String sistema) {
    List lista = new ArrayList(1);
    for (FuncionRolTO tipp : funcionestodas) {
      if (tipp.getSistema().trim().equalsIgnoreCase(sistema)) {
        lista.add(tipp);
      }
    }
    return lista;
  }
       private void llenarComboSistema() {

        List listaTipos_cbo = this.cboBenaBindingSistema1.getLista();
        listaTipos_cbo.clear();
        try
        {
         listasistema = GestionSeguridadDelegate.getInstance().getListaSistema();

          if(!listasistema.isEmpty()){
              this.cboBenaBindingSistema1.setLista(listasistema);
              this.bindingGroup.bind();
              this.jcbSistema.updateUI();
              this.jcbSistema.setSelectedIndex(-1);
          }
          else{
              this.jcbSistema.removeAllItems();
          }
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener sistemas\n" + ex.getMessage());
          ex.printStackTrace();
          Logger.getLogger(jdAdministrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
        }
      }


     private void obtenerFunciones() {
         String sistema= this.jcbSistema.getSelectedItem().toString();
         if(sistema!=null){
             this.funciones = getFuncion_sistema(sistema);
         }
         else{
         }
         if(funciones.isEmpty()){
                Utiles_MensajesDialogo.mensajeAlerta("No se han encontrado funciones");
         }
         cargarDatos();

  }
     public void cargarDatos(){
    this.bindingGroup = new BindingGroup();
    this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, this.funciones, this.jtFunciones);

    JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${descripcion}"));

    columnBinding0.setColumnName("DESCRIPCION DE FUNCION");
    columnBinding0.setColumnClass(String.class);
    columnBinding0.setEditable(false);

    this.bindingGroup.addBinding(this.jTableBinding);
    this.bindingGroup.bind();
    int[] porcentajes= {100};
        try {
            Utiles_Tabla.definirAnchoColumnasJTable(this.jtFunciones, porcentajes);
        } catch (Exception ex) {
            Logger.getLogger(jdAdministrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
        }
  }
     public void grabarfuncion(){

         int fila=this.jtFunciones.getSelectedRow();
         if (fila < 0) {
                Utiles_MensajesDialogo.mensajeError("Debe elegir una funcion");
                return;
            }
         else{
           TblBeanBindingFunciones funcionto= new TblBeanBindingFunciones();
           funcionto.setDescripcion(this.jtFunciones.getValueAt(fila, 0).toString());
           funcionto.setSistema(this.jcbSistema.getSelectedItem().toString());
           funcionto.setCodsistema(this.obtenerSistema(this.jcbSistema.getSelectedItem().toString()).getCodigo());
           funcionto.setCodFuncion(this.obtenerFuncion(funcionto.getDescripcion()).getCodigo());
           try {
                padre.agregaDetalle(funcionto);
            } catch (Exception ex) {
                Logger.getLogger(jdAdministrarFuncion.class.getName()).log(Level.SEVERE, null, ex);
            }
           padre.actualizaTablaDetalle();
         }
     }
      public SistemaTO obtenerSistema(String nombre){
        SistemaTO sis= new SistemaTO();
        for(int i=0 ; i<this.listasistema.size(); i++){
            if((((SistemaTO)listasistema.get(i)).getDescripcion().trim()).compareTo(nombre)==0){
                sis= (SistemaTO)listasistema.get(i);
            }
        }
        return sis;
    }
      public FuncionRolTO obtenerFuncion(String nombre){
        FuncionRolTO rol= new FuncionRolTO();
        for(int i=0 ; i<this.funciones.size(); i++){
            if((((FuncionRolTO)funciones.get(i)).getDescripcion().trim()).compareTo(nombre)==0){
                rol= (FuncionRolTO)funciones.get(i);
            }
        }
        return rol;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBenaBindingSistema1 = new administracion.BeanBinding.CboBenaBindingSistema();
        jbCancelar1 = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jcbSistema = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtFunciones = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdAdministrarFuncion.class);
        jbCancelar1.setFont(resourceMap.getFont("jbCancelar1.font")); // NOI18N
        jbCancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar1.setMnemonic('C');
        jbCancelar1.setText(resourceMap.getString("jbCancelar1.text")); // NOI18N
        jbCancelar1.setName("jbCancelar1"); // NOI18N
        jbCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelar1ActionPerformed(evt);
            }
        });

        jbAceptar.setFont(resourceMap.getFont("jbCancelar1.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/apply.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jLabel1.setName("jLabel1"); // NOI18N

        jcbSistema.setName("jcbSistema"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBenaBindingSistema1, eLProperty, jcbSistema);
        bindingGroup.addBinding(jComboBoxBinding);

        jcbSistema.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbSistemaItemStateChanged(evt);
            }
        });
        jcbSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbSistemaActionPerformed(evt);
            }
        });
        jcbSistema.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbSistemaKeyPressed(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 12, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jtFunciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DESCRIPCION DE FUNCION"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtFunciones.setName("jtFunciones"); // NOI18N
        jtFunciones.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtFuncionesKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jtFunciones);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(jcbSistema, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(183, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar1))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbAceptar, jbCancelar1});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbSistema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3))
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar1))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelar1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbCancelar1ActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        this.grabarfuncion();
}//GEN-LAST:event_jbAceptarActionPerformed

    private void jcbSistemaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbSistemaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.requestFocus();
        }
}//GEN-LAST:event_jcbSistemaKeyPressed

    private void jcbSistemaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbSistemaItemStateChanged

    }//GEN-LAST:event_jcbSistemaItemStateChanged

    private void jtFuncionesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtFuncionesKeyPressed
       if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.requestFocus();
        }
    }//GEN-LAST:event_jtFuncionesKeyPressed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
    }//GEN-LAST:event_jbAceptarKeyPressed

    private void jcbSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbSistemaActionPerformed
        if(this.jcbSistema.getSelectedIndex()>=0)
           this.obtenerFunciones();
    }//GEN-LAST:event_jcbSistemaActionPerformed

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private administracion.BeanBinding.CboBenaBindingSistema cboBenaBindingSistema1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar1;
    private javax.swing.JComboBox jcbSistema;
    public javax.swing.JTable jtFunciones;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
